<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="客户ID，主键" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                        <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
                </el-form-item>
                <el-form-item label="联系人姓名" prop="contactPerson">
                        <el-input v-model="dataForm.contactPerson" placeholder="联系人姓名"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="contactPhone">
                        <el-input v-model="dataForm.contactPhone" placeholder="联系电话"></el-input>
                </el-form-item>
                <el-form-item label="联系邮箱" prop="contactEmail">
                        <el-input v-model="dataForm.contactEmail" placeholder="联系邮箱"></el-input>
                </el-form-item>
                <el-form-item label="客户分类" prop="customerCategory">
                        <el-input v-model="dataForm.customerCategory" placeholder="客户分类"></el-input>
                </el-form-item>
                <el-form-item label="客户分级" prop="customerLevel">
                        <el-input v-model="dataForm.customerLevel" placeholder="客户分级"></el-input>
                </el-form-item>
                <el-form-item label="客户信息创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="客户信息创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户信息更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="客户信息更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户地址" prop="address">
                        <el-input v-model="dataForm.address" placeholder="客户地址"></el-input>
                </el-form-item>
                <el-form-item label="业务往来历史" prop="businessHistory">
                        <el-input v-model="dataForm.businessHistory" placeholder="业务往来历史"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    customerId: null,
                    customerName: null,
                    contactPerson: null,
                    contactPhone: null,
                    contactEmail: null,
                    customerCategory: null,
                    customerLevel: null,
                    createTime: null,
                    updateTime: null,
                    address: null,
                    businessHistory: null,
                },
                dataRule: {
                    customerId: [
                        { required: true, message: '客户ID，主键不能为空', trigger: 'blur' }
                    ],
                    customerName: [
                        { required: true, message: '客户名称不能为空', trigger: 'blur' }
                    ],
                    contactPerson: [
                        { required: true, message: '联系人姓名不能为空', trigger: 'blur' }
                    ],
                    contactPhone: [
                        { required: true, message: '联系电话不能为空', trigger: 'blur' }
                    ],
                    contactEmail: [
                        { required: true, message: '联系邮箱不能为空', trigger: 'blur' }
                    ],
                    customerCategory: [
                        { required: true, message: '客户分类不能为空', trigger: 'blur' }
                    ],
                    customerLevel: [
                        { required: true, message: '客户分级不能为空', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '客户信息创建时间不能为空', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '客户信息更新时间不能为空', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '客户地址不能为空', trigger: 'blur' }
                    ],
                    businessHistory: [
                        { required: true, message: '业务往来历史不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomerInfo/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appcustomerinfo
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppCustomerInfo/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>